package com.itawei.d2_UDP3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Scanner;

/**
 * UDP三种模式：单播 组播 广播
 */
public class SendMessage {
    public static void main(String[] args) throws IOException {

        //发送数据
        //1.创建发送端的DatagramSocket对象
        MulticastSocket ms = new MulticastSocket();
        //2.创建数据，并把数据打包
        Scanner sc = new Scanner(System.in);
        while (true) {

            System.out.println("请输入要说的话：");
            String str = sc.next();//要发送的文字用键盘输入
            byte[] bytes = str.getBytes();
            if("886".equals(str)){
                break;
            }
            //要发送的地址
            InetAddress address = InetAddress.getByName("224.0.0.1");
            //要发送的端口
            int port = 10086;

            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
            //3.发送数据
            ms.send(dp);
        }

        //4.释放资源
        ms.close();
    }
}
